﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;
namespace DTcms.Web.m
{
    /// <summary>
    /// xinxi_add 的摘要说明
    /// </summary>
    public class xinxi_add : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }

            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            ///用户状态判断  lihy 20200702 添加用户状态判断，如果用户禁用，无法发布信息
            ///正常 0 ；
            ///待验证 1； 
            ///待审核 2； 
            ///禁用 3
            if (userModel.status == 3) {
                context.Response.Write("{\"status\":0, \"msg\":\"您的账号状态异常，无法发布信息，请联系管理员！\"}");
                return;
            }

            //#####根据用户会员级别控制发布现货 库存的总数量
            //##### 2018 11 01
            int _type_id = DTRequest.GetFormInt("txtTypeID", 0);
            BLL.users blluser = new BLL.users();
            BLL.user_groups bllgroup = new BLL.user_groups();
            int userAddCount = bllgroup.GetgongqiuAdd(userModel.group_id);//获取标准数量
            string tips = "";

            #region 绑定 店铺ID
            int _company_id = 0;
            BLL.company bllcom = new BLL.company();
            _company_id = bllcom.GetCompanyID(userModel.id);
            #endregion

            BLL.xinxi bll = new BLL.xinxi();

            //####添加短时间内发布限制 180秒内不能发布超过3条 ，暂不限类型
            //####Demon 2020-03-09
            //int xcount= bll.GetNumByUser_idAndType_Idandtime(userModel.id, _type_id, 180);//限制类型，当前添加的类型 180秒内不能超过3条
            int xcount = bll.GetNumByUser_idAndType_Idandtime(userModel.id, 0, 180);
            Utils.WriteTextLog("xinxi_add_"+ user_name, "username:" + user_name + ",pwd:" + user_pwd + "/r/n180秒内添加得信息数量：" + xcount+"/r/n 类型 "+_type_id , DateTime.Now);
            if (xcount >= 3) {
                context.Response.Write("{\"status\":0, \"msg\":\"操作过于频繁,请稍后再试\"}");
                return;
            }


            //###普通会员发布现货
            //###认证会员发布现货
            //###钻石会员发布现货
            //### Demon 2018-11-1
            //####  _type_id  信息类型 1 石材现货  2 求购 3 圈子生活 
            //####  调整 为统计发布现货的总数量  按总数量计算，不再按每天多少计算 2018-11-02
            if (_type_id == 1)
            {
                ///获取用户已发布 石材现货的总条数
                int xinxicount = bll.GetNumByUser_idAndType_Id(userModel.id, _type_id);
                if (xinxicount >= userAddCount)
                {
                    //Demon 20200304 修改 提示信息由原来获取认证会员 价格与条数修改为获取VIP会员的价格与条数
                    Model.user_groups ugmodel = bllgroup.GetModelByGrage(3);
                    int usergroup = ugmodel.price;
                    int gongqiu_add = ugmodel.gongqiu_add;
                    if (userModel.group_id < 3) {
                        tips = "免费用户可发布五条库存信息，升级vip会员并提交认证，活动仅需" + usergroup + "元   即可不限制发布现货信息！";
                    }
                    else { tips = "发布石材现货次数已用完！"; }
                    Utils.WriteTextLog("xinxi_add_" + user_name, "username:" + user_name + ",pwd:" + user_pwd + "/r/n" + tips, DateTime.Now);
                    context.Response.Write("{\"status\":0, \"msg\":\"" + tips + "\"}");
                    return;
                }

                blluser.UpdateField(userModel.id, "releasecount=releasecount+1,releasetime=getdate()");
            }
            Model.xinxi model = new Model.xinxi();

            int _category_id = DTRequest.GetFormInt("ddlCategoryID", 0);
            //int _type_id = DTRequest.GetFormInt("txtTypeID", 0); 类型 迁移到上面做判断  发布现货 有次数限制 其他 没有次数限制
            int _long = DTRequest.GetFormInt("txtLong", 0);
            int _wide = DTRequest.GetFormInt("txtWide", 0);
            int _height = DTRequest.GetFormInt("txtHeight", 0);
            string _remark = DTRequest.GetFormString("txtRemark");
            int _quanzi_id = DTRequest.GetFormInt("txtQuanziID", 1);

            if (_type_id < 3)
            {
                if (_long == 0)
                {
                    context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入长度！\"}");
                    return;
                }
                if (_wide == 0)
                {
                    context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入宽度！\"}");
                    return;
                }
                if (_height == 0)
                {
                    context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入高度！\"}");
                    return;
                }
            }
            if (string.IsNullOrEmpty(_remark))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入内容！\"}");
                return;
            }
            model.company_id = _company_id;
            model.category_id = _category_id;
            model.type_id = _type_id;
            model.size_long = _long;
            model.size_wide = _wide;
            model.size_height = _height;
            model.content = _remark;
            model.add_time = DateTime.Now;
            model.status = 1;
            model.user_id = userModel.id;
            model.lianxiren = userModel.user_name;
            model.mobile = userModel.mobile;
            model.quanzi_id = _quanzi_id;

            #region 保存相册====================
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                List<Model.xinxi_albums> ls = new List<Model.xinxi_albums>();
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile myFile = files[i];
                    string thumb_path = FileSave(context, myFile, true);
                    string filePath = thumb_path.Replace("thumb_", "");
                    ls.Add(new Model.xinxi_albums { original_path = filePath, thumb_path = thumb_path });
                    //设置默认图片
                    if (i == 0)
                    {
                        model.img_url = thumb_path;
                    }
                }
                model.albums = ls;
            }
            #endregion

            if (bll.Add(model) > 0)
            {
                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，内容提交成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }

        /// <summary>
        /// 统一保存文件
        /// </summary>
        private string FileSave(HttpContext context, HttpPostedFile upFiles, bool isWater)
        {
            //获取文件信息
            string fileName = upFiles.FileName;
            byte[] byteData = FileHelper.ConvertStreamToByteBuffer(upFiles.InputStream); //获取文件流
            //开始上传
            string remsg = new UpLoad().FileSaveAs(byteData, fileName, true, isWater);
            Dictionary<string, object> dic = JsonHelper.DataRowFromJSON(remsg);
            string status = dic["status"].ToString();
            string msg = dic["msg"].ToString();
            if (status == "0")
            {
                //showError(context, msg);
                return "";
            }
            string filePath = dic["thumb"].ToString(); //取得上传后的路径
            return filePath;
            //showSuccess(context, fileName, filePath); //输出成功提示
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}